//
//  YBSearchCollectionViewController.m
//  OurTourism
//
//  Created by dllo on 15/11/24.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "YBSearchCollectionViewController.h"
#import "searchCollectionViewCell.h"
#import "SearchTitleCollectionReusableView.h"
#import "MZRecommendThirdViewController.h"
#import "MZRecommendPushViewController.h"
#import "YBDestinationTableViewcell.h"
#import "ybsearchdetialViewController.h"

@interface YBSearchCollectionViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate>

@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UICollectionViewFlowLayout *layout;
@property (nonatomic, retain)UISearchBar *searchBar;
@end

@implementation YBSearchCollectionViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
    [self SearchBar];
  
    
    
}



- (void )createView
{
    self.layout = [[UICollectionViewFlowLayout alloc]init];
    
    self.layout.itemSize = CGSizeMake((WIDTH - 40) / 3, 30);
    self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.layout.headerReferenceSize = CGSizeMake(WIDTH, WIDTH / 7);
    
    //创建collectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.bounds.size.height) collectionViewLayout:self.layout];
    //滑动视图距离根视图内容边界
    // self.collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    //collectionView 注册cell
    [self.collectionView registerClass:[searchCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerClass:[SearchTitleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
}



#pragma mark - 创建searchBar
- (void)SearchBar
{
    // 初始化
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    // 设置代理
    self.searchBar.delegate = self;
    //类型
    [self.searchBar setBarStyle:UIBarStyleDefault];
    //提示文本
    self.searchBar.placeholder = @"搜索目的地  游记...";
    
    //键盘类型
    [self.searchBar setKeyboardType:UIKeyboardTypeDefault];
    
    [self.searchBar becomeFirstResponder];
    //视图剪切
    self.searchBar.layer.masksToBounds = YES;
    self.searchBar.layer.cornerRadius = 10;
    
    //关闭用户交互
    self.searchBar.userInteractionEnabled = YES;
    self.navigationItem.titleView =  self.searchBar;
    
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark - searchBar 触发方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    ybsearchdetialViewController *search = [[ybsearchdetialViewController alloc]init];
    search.text = self.searchBar.text;
    [self.navigationController pushViewController:search animated:YES];
    
}
//
//#pragma mark - 按钮触发方法
//- (void)backToLast
//{
//    [self.navigationController popViewControllerAnimated:YES];
//    
//}



#pragma mark - 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        SearchTitleCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.model = self.modelSearchArr[indexPath.section];
        
        return header;
        
    }
    return nil;
}

#pragma mark - 分行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return [self.modelSearchElementsArr[section] count];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    searchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.modelSearchElementsArr[indexPath.section][indexPath.item];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"撒打算 ");
    
    if (indexPath.section == 0) {
      
        
        // 判断
        if ([((YBDestinationModel *)self.modelSearchElementsArr[indexPath.section][indexPath.row]).type  isEqual: @5]) {
           
            MZRecommendThirdViewController *third = [MZRecommendThirdViewController alloc];
            
            third.tempModel = self.modelSearchElementsArr[indexPath.section][indexPath.row];
            
            [self.navigationController pushViewController:third animated:YES];
         
            
        }else {
            MZRecommendPushViewController *four = [MZRecommendPushViewController alloc];
            four.tempModel =self.modelSearchElementsArr[indexPath.section][indexPath.row];
            [self.navigationController pushViewController:four animated:YES];
            
         
            
        }
        
   
    }
    else
    {
        // 判断
        if ([((YBDestinationModel *)self.modelSearchElementsArr[indexPath.section][indexPath.row]).type  isEqual: @5]) {
            
            MZRecommendThirdViewController *third = [MZRecommendThirdViewController alloc];
            
            
            third.tempModel = self.modelSearchElementsArr[indexPath.section][indexPath.row];
            [self.navigationController pushViewController:third animated:YES];
            
        
        }else {
            MZRecommendPushViewController *third = [MZRecommendPushViewController alloc];
            
             third.tempModel = self.modelSearchElementsArr[indexPath.section][indexPath.row];
          
            [self.navigationController pushViewController:third animated:YES];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
